// example/sign/sign.js
var app = getApp()
import * as Api from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColors: '',
    hasGoneAway: false,

    loginName: "",
    password: "",
    verifyInfo: '获取验证码',
    isdisable: false,
    contactMobile: '',
    code: '',//验证码

    index: 3,
    array: ['车主', '配件商', '修理厂'],
    objectArray: [
      {
        id: 0,
        name: '车主'
      },
      {
        id: 1,
        name: '配件商'
      },
      {
        id: 2,
        name: '修理厂'
      },
      {
        id: 3,
        name: '请选择会员身份'
      },
    ],
  },
  goLogin() {

    if (!this.data.hasGoneAway) {
      this.setData({ hasGoneAway: true })
      wx.redirectTo({
        url: '../login/login',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }

  },
  vaild() {
    var that = this
    if (this.data.loginName.length!==11) {
      wx.showModal({
        title: '',
        content: '手机号格式错误',
        showCancel: false,
        cancelText: '',
        cancelColor: '',
        confirmText: '知道了',
        confirmColor: '',
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
      return;
    } else {

      if (that.data.isdisable == false) {
        wx.showLoading({
          title: '加载中',
          mask: true
        })
        that.getCode()

      }
    }


  },
  getCode() {
    let self = this
    let url = app.globalData.baseUrl1 + '/auth/getsmscode'
    let data = {
      mobile: this.data.loginName,
      type: 'SMS_118165006'
    }
    let callBack = (res) => {

      if (res == null) {
        console.log("网络请求错误1！");
        return;
      }
      if (res.data.status != 200) {
        wx.showModal({
          title: res.data.data,
          content: '',
          showCancel: false,
          cancelColor: '',
          confirmColor: '',
          success: function (res) { },
          fail: function (res) { 
            
          },
          complete: function (res) {

           },
        })
        return;
      }

      if (res.data.status == 200) {
        var count = 60;
        var timer = setInterval(function () {
          count--;
          if (count >= 1) {
            self.setData({
              verifyInfo: count + 's'
            })
          } else {
            self.setData({
              verifyInfo: '获取验证码'
            })
            clearInterval(timer);
            self.data.isdisable = false;
          }
        }, 1000);
        self.data.isdisable = true;
        wx.showToast({
          title: '发送成功',
          icon: 'success',
          duration: 2000
        })

      }else{
        wx.showModal({
          title: res.data.message,
          content: '',
          showCancel: false,
          cancelColor: '',
          confirmColor: '',
          success: function (res) { },
          fail: function (res) {

          },
          complete: function (res) {

          },
        })
      }
      wx.hideLoading()
    }
    Api.reqFormPost(url, data, callBack)

  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },


  sign() {
    let self = this

    let role = ''
    switch (this.data.array[this.data.index]) {
      case '车主': role = 'OWNER'; break;
      case '配件商': role = 'PARTS'; break;
      case '修理厂': role = 'REPAIR'; break;
    }
    if (!this.data.loginName.length ===11 && !this.data.password.length > 0) {
      return
    }



      wx.getUserInfo({
        success: (res2) => {
          app.globalData.userInfo = res2.userInfo
          let url = app.globalData.baseUrl1 + '/auth/register';
          let data = {
            loginName: this.data.loginName,
            password: this.data.password,
            code: this.data.code,
            role: role,
            merchantId: app.globalData.merchant.merchantId,
            nickName: app.globalData.userInfo.nickName,
            iconUrl: app.globalData.userInfo.avatarUrl,
            openId: app.globalData.openid
          }
          let callBack = (res) => {

            if (res == null) {

              console.log("网络请求错误！");
              return;
            }

            if (res.data.status != 200) {
              wx.showModal({
                title: '',
                content: res.data.message,
                showCancel: false,
                cancelText: '',
                cancelColor: '',
                confirmText: '知道了',
                confirmColor: '',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
              return;
            }
            if (res.data.status === 200) {
              console.log('注册成功', res)



              if (res.data.data.userStatus === 'USER_CHECK_SUCCEED') {
                app.globalData.userId = res.data.data.userId
                app.globalData.token = res.data.data.token

                wx.showToast({
                  title: '注册成功',
                  icon: 'siuccess',
                  duration: 2000,
                  mask: true,
                  success: function (res) {
                    wx.redirectTo({
                      url: '../login/login',
                      success: function (res) { },
                      fail: function (res) { },
                      complete: function (res) { },
                    })
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              } else if (res.data.data.userStatus === 'USER_UNCHECK') {
                wx.showModal({
                  title: '',
                  content: '注册已提交,需等待后台审核',
                  showCancel: false,
                  cancelText: '',
                  cancelColor: '',
                  confirmText: '知道了',
                  confirmColor: '',
                  success: function (res) {
                    if (!self.data.hasGoneAway) {
                      self.setData({ hasGoneAway: true })

                      wx.navigateBack({
                        delta: 1,
                      })
                    }
                  },
                  fail: function (res) { },
                  complete: function (res) { },
                })
              }
            }
          }
          Api.reqFormPost(url, data, callBack)
        }
      });
  },
  changeInputCode(e) {
    this.setData({
      code: e.detail.value
    })
  },
  changeInputContactMobile(e) {
    this.setData({
      contactMobile: e.detail.value
    })
  },

  changeInputLoginName(e) {
    this.setData({
      loginName: e.detail.value
    })
  },
  changeInputPassword(e) {
    this.setData({
      password: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (app.globalData.merchant === null) {
      app.doLogin(this.onLoad, this.onShow, options);
      return
    }
    this.setData({
      themeColors: app.globalData.merchant.themeColors
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.merchant === null) {
      return
    } 
    this.setData({ hasGoneAway: false })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})